package com.xtwy.qrcodescan.zxing.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.Build;
import android.util.Log;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: InactivityTimer.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述:
 * 创建时间: 2018/9/14 10:03
 * 更新时间：2018/9/14 10:03
 * -----------------------------------------------------------------
 * 2018/9/14 10:03: 创建 InactivityTimer.java (侯建军);
 * -----------------------------------------------------------------
 */
public class InactivityTimer {

    static final String TAG = InactivityTimer.class.getSimpleName();

    static final long INACTIVITY_DELAY_MS = 5 * 60 * 1000L;

    Activity activity;
    BroadcastReceiver powerStatusReceiver;
    boolean registered;
    AsyncTask<Object, Object, Object> inactivityTask;

    public InactivityTimer(Activity activity) {
        this.activity = activity;
        powerStatusReceiver = new PowerStatusReceiver();
        registered = false;
        onActivity();
    }

    @SuppressLint("NewApi")
    public synchronized void onActivity() {
        cancel();
        inactivityTask = new InactivityAsyncTask();
        if (Build.VERSION.SDK_INT >= 11) {
            inactivityTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            inactivityTask.execute();
        }
    }

    /**
     * 任务暂停
     */
    public synchronized void onPause() {
        cancel();
        if (registered) {
            activity.unregisterReceiver(powerStatusReceiver);
            registered = false;
        } else {
            Log.w(TAG, "PowerStatusReceiver从未注册过?");
        }
    }

    /**
     * 任务恢复
     */
    public synchronized void onResume() {
        if (registered) {
            Log.w(TAG, "PowerStatusReceiver 已经注册?");
        } else {
            activity.registerReceiver(powerStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            registered = true;
        }
        onActivity();
    }

    /**
     * 任务取消
     */
    synchronized void cancel() {
        AsyncTask<?, ?, ?> task = inactivityTask;
        if (task != null) {
            task.cancel(true);
            inactivityTask = null;
        }
    }

    /**
     * 关机
     */
    public void shutdown() {
        cancel();
    }

    /**
     * 电源状态广播
     */
    class PowerStatusReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                // 0 indicates that we're on battery
                boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
                if (onBatteryNow) {
                    InactivityTimer.this.onActivity();
                } else {
                    InactivityTimer.this.cancel();
                }
            }
        }
    }

    class InactivityAsyncTask extends AsyncTask<Object, Object, Object> {
        @Override
        protected Object doInBackground(Object... objects) {
            try {
                Thread.sleep(INACTIVITY_DELAY_MS);
                Log.i(TAG, "Finishing activity due to inactivity");
                activity.finish();
            } catch (InterruptedException e) {
                // continue without killing
            }
            return null;
        }
    }
}

